package xin.config.swagger;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import xin.core.AppConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * Swagger2 config
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 18:03
 */

@EnableWebMvc
@EnableSwagger2 // 使swagger2生效
@Configuration // 配置注解，自动在本类上下文加载一些环境变量信息
public class Swagger2 extends WebMvcConfigurationSupport {

    @Bean
    public Docket createYApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(AppConstant.JWT_HEADER).description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        List<Parameter> operationParameters = new ArrayList<>(1);
        operationParameters.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoXin()).select()
                .apis(RequestHandlerSelectors.basePackage("xin"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build()
                .globalOperationParameters(operationParameters);
    }

    private ApiInfo apiInfoXin() {
        return new ApiInfoBuilder().title("xin Rest API").version("1.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html").license("Apache 2.0")
                .description("此API为开发人员，测试人员，UI人员提供方便快捷的API开发，测试体验").build();
    }
}

